package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116658219";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCti35h4Y94jWwa1rYNNMdzlKAQnDF2tNsoEDUiftXQ3DpTnV0OAB5xyVXbW9ynFiQC+L/evlsV3prww9WoMI+dhI3akwZo0eEMcOxNJzimSMFxZJFGefeickXcV2q8IrXjDuwWGJsz7PWn3tIaEWb0Ic5VNVT+5DhakguIj6U/GIR16WgYxuYZc//ZYfYJP4Zv2S4csibbU9xvG0daBWqg4t6B0ETZz5GeBWs4iSUXEh/ClWrg1ASfEI+unmoTmDNv7DSGLaVEH/7ASC+5E+0vxlEhlmDFqm3phmjh8mniUCIiYccS0AhXwMj4Yo4xnUDWluSxKnsqxXnErmiMa4S7AgMBAAECggEBAIaB5oug0p1iJ1bJ8cJclPYtTL5zARd7Z50uqrA41CoUdxSVlCkkyHPy05u4IFZUn8a526Sdr2VM6pQPhQrd0IIK2hN9/urBOQd2gn/uFUc0ftIPW1XfdKKwpRr5mh2FXapFx4/8tFZTo7bfycIwmLZBeXWrZ1xDsLXqGgxXIYG7iIj+znueYKdydXGNcyjI3Q6ZruVBCTLfHgQR96UPkpaVr6wpmh2ECe8RJ72ACW1r+uweNXzqP8BTxcMA7aTVbODjx7ue7ZPHQfB7ybGVJxjPd9hV0+IZxiO/TUf6ABNGk5mvYKLnL0R79d/0wc18chqXJXjslDkq7uWYO34VTdECgYEA+amME8uqmx/VLEOATvOD9hHCcrvaNv71zJlfvF2GaHMBDDKIZBBK6zsKQ8NiWDE0EBUgJp1uMlOHxvrxnyS4EWsUifIk/Cs0cVAR0hwW8WZUOumIn0kTPXQWp9paPG2SpZOivJjHr3I4J3MNiR2LCwocpJIPYHZ7a94Dd3zNq/kCgYEAsfNKb/4+CyGFPVof0iTyqmQc3Os+XWr6mw9sjKtjcn80OiG2xh2Y9Auf5z3Ld+IV4Y1eW38WEThMruI9Z0LIrcXXPrAVF8eByEE/SlbDdICANdHltTrnFxH/bONAJBNPrbcTS7isi5RFD0T1snBBCutcTu2jqzf5o0uOHPjFm1MCgYA6EG6do/53uX2Qn9pVglksf5DjskDcDYwJG5qianX6ENUriw0hiJCECOUoQWa78CWVn/QsRuz8E2U+p9pOEvIkSu0p8AEeDMZTnDWo0hOb7Rql4O5/bTVmU3t5alUWPL2geKeeiUHX0g6xThbVzuMpnB5xP+8Hlka6HZZc0c/bsQKBgQCIyXQ5vFxi4AR0kbeOKLqkQcpyAx5wXtB+qw+M3h3WoHM8EDhX995gEiN7dGPYJmEdfTa/7GPBHt7LO7tpicuK0F5RoLbGDaT0kzscYK9j+XcUZ+FEOx/x+vK198WRNYr1BJp/hViU/odLYn3fCSzmQN83CLLb5W4umOCH4q67YwKBgG9wRO5+rn2jt+dKIqaeLpmNznILW7LkeFCjfAwkJiXEpXyJJHlKg/w2DaZZtOEfispF+FB8SGo47DifaGeHlpC+OMiHieVXNQUZVR+IdNiUyix9NPSNiYl3LAHPhcfTQbXZFb2J2fPUsg3z+JpiE9SaYrkWGxBhhB2PXjqiCAgM";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnwOtdSdgoKiOqR3A4qiQYyK4gKI/BVXI/Lk8cD3nvTTH4TjJVMdmr4tkBfTZrUp/KPfSWl7lRhbdp/SZbfAfnNu6ZUru0HXtgkCjZ6d5qSlwjp83/ue0oCm6oOB0oCtRHhFOuh+ipqHN62RIfti7yeRFwmbVh6AiVRbq64GnDW6y0O1Nc1ZWTsWQ3xDah6w+Qk++D5FW2ALEYZbo6EFyO4Q5xAp2VWYZHv+SPJtPTZtpsff7UAtQg2j9FeXpH0+84hXSuqqPZlOGO4JRpl1dVvnQVwsvC2himjPz0sv+HOKXrw1ZiN4WeSZ/8rGjmLd1eKSDgv6woM/wUGhCtgWHCwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://bn1s5cgtbm.54http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
